package zsc.nine.onlinemall.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zsc.nine.onlinemall.entity.Product;
import zsc.nine.onlinemall.mapper.ProductMapper;

import java.util.List;

/**
 * @ClassName: ProductService
 * @Description: Service of Product operation
 * @Author: Zainus
 * @CreateDate: Jul.27, 2021
 * @UpdateDate: Jul.27, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: ProductService
 * @描述: 产品管理服务类
 * @创建人: 叶增钥
 * @创建时间: 2021年7月27日
 * @更新时间: 2021年7月27日
 * @版本: [v1.0]
 */
@Component
public class ProductService {

    @Autowired
    ProductMapper productMapper;

    public boolean createProduct(String name, String image, String originPrice, String nowPrice, String stock) {

        Product product = new Product();
        product.setName(name);
        product.setImage(image);
        product.setOriginPrice(Double.parseDouble(originPrice));
        product.setNowPrice(Double.parseDouble(nowPrice));
        product.setStock(Integer.parseInt(stock));
        product.setStatus(0); //未上架

        return productMapper.insertProduct(product);

    }

    public boolean deleteProduct(String id) {
        return productMapper.deleteAProduct(Integer.parseInt(id));
    }

    public Product findProductById(String id) {
        return productMapper.findProductById(Integer.parseInt(id));
    }

    public boolean productExist(String id) {
        return productMapper.findProductById(Integer.parseInt(id)) != null;
    }

    public boolean productOnSale(String id) {
        return productMapper.updateStatusById(Integer.parseInt(id), 1);

    }

    public boolean productOffSale(String id) {
        return productMapper.updateStatusById(Integer.parseInt(id), 0);
    }

    public List<Product> pagedFindById(String index, String pageSize) {
        return productMapper.pagedFindByIdAsc(Integer.parseInt(index), Integer.parseInt(pageSize));
    }

    public String getLength() {
        return productMapper.getLength();
    }

    public List<Product> findByLikeName(String likeName) {
        return productMapper.findByLikeName(likeName);
    }

    public List<Product> getAll(){
        return productMapper.findAllProduct();
    }
}
